<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root">

    </div>
</body>
<script src="../react@17.js"></script>
<script src="../react-dom@17.js"></script>
<script src="../babel.js"></script>
<script type="text/babel">
    class App extends React.Component{
        state = {
            msg:"奔跑吧！！！少年！！！！",
            timer:null
        }
        render(){
            let {msg} = this.state;
            return (
                <div>
                    <h1>{msg}</h1>    
                    <button onClick={this.start}>开始</button> | <button onClick={()=>{
                        clearInterval(this.state.timer);
                        this.setState({
                            timer:null
                        })
                    }}>暂停</button>
                </div>
            )
        }
        start=()=>{
           let {timer} = this.state; 
           if(timer){
                return
           }
           timer = setInterval(() => {
                let {msg} = this.state;
                // 截取msg第一个字符
                let str1 = msg.substring(0,1);
                // 截取剩下字符
                let str2 = msg.substring(1)
                // 组成最新msg
                let newMsg = str2+str1;
                console.log(newMsg)
                this.setState({
                    msg:newMsg
                })
            }, 50);
            this.setState({
                timer
            })
        }
    }

    ReactDOM.render(<App/>,document.getElementById("root"))
</script>
</html>